package com.crm.consult.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crm.annotation.SystemServiceLog;
import com.crm.model.BizSysuserCitys;
import com.crm.model.ConsultInfoManage;
import com.crm.model.SysUser;
import com.crm.service.BizSysuserCitysService;
import com.crm.service.ConsultInfoManageService;
import com.crm.service.SysDepartmentService;
import com.crm.service.SysUserService;
import com.crm.util.AopConfig;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.SessionModel;
import com.crm.util.SessionUtils;

@Controller
@RequestMapping("/consultDistributeCenter")
public class ConsultDistributeCenterController {
	
	@Autowired
	private SysDepartmentService sysDepartmentService;
	@Autowired
	private BizSysuserCitysService bizSysuserCitysService;
	@Autowired
	private ConsultInfoManageService consultInfoManageService;
	@Autowired
	private SysUserService sysUserService;
	/**
	 * 
	 * @Description : 跳转页放入部门信息
	 * @author zealot
	 * @param @param request
	 * @param @return
	 * @return String
	 * @version 创建时间 : 2016年11月28日 下午2:52:11
	 * @thorws
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request){  
		SysUser sy = (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		if(sy==null){ 
			return "tologin";    
		}
//		SysDepartment sd = sysDepartmentService.selectByPrimaryKey(sy.getCompanyId());
		if(sy!=null){
			if("3".equals(sy.getDepType())){
				BizSysuserCitys bsc = new BizSysuserCitys();
				bsc.setDepartmentId("'"+sy.getCompanyId()+"'");
				List<SysUser> userList = getUsers();
				request.setAttribute("selectList", userList);
				String ar = "";
				for(SysUser e:userList){
					ar = ar+"'"+e.getUserId()+"',";
				}
				ar = ar+"'"+sy.getUserId()+"'";
				request.setAttribute("dep", ar);
			}else{
				List<SysUser> userList = getUsers();
				String ar = "";
				for(SysUser e:userList){
					ar = ar+"'"+e.getUserId()+"',";
				}
				ar = ar+"'"+sy.getUserId()+"'";
				request.setAttribute("dep", ar);
				request.setAttribute("selectList", userList);
			}
		}else{
			BizSysuserCitys bsc = new BizSysuserCitys();
			request.setAttribute("selectList", getUsers());
			request.setAttribute("dep", sy.getDepartmentId());
		}
		return "consultcenter/infoDistribution";
	}
	
	/**
	 * 
	 * @Description : 以职位为条件检索出用户群体
	 * @author zealot
	 * @param @return
	 * @return List<SysUser>
	 * @version 创建时间 : 2016年12月20日 下午3:53:56
	 * @thorws
	 */
	public List<SysUser> getUsers(){
		SysUser sy = (SysUser)SessionUtils.get(SessionModel.SESSION_USER);
		return sysUserService.selectByDuty(sy);
	}
	
	/**
	 * 
	 * @Description : 人工分配逻辑
	 * @author zealot
	 * @param @param response
	 * @param @param request
	 * @param @param ar
	 * @param @param counselor
	 * @param @param counselorId
	 * @return void
	 * @version 创建时间 : 2016年11月29日 下午3:44:35
	 * @thorws
	 */
	@SystemServiceLog(description=AopConfig.CONSULTINFOASSIGNED+AopConfig.ASSIGNED,type=AopConfig.ASSIGNED)
	@RequestMapping("/toChooseAr")
	public void toChooseAr(HttpServletResponse response,HttpServletRequest request,String ar,String counselor,String counselorId,String status){
		if(ar!=null){
			String[] ary = ar.split(",");
			ConsultInfoManage cim = null;
			for(String e : ary){
				cim = new ConsultInfoManage();
				cim.setCounselor(counselor);
				cim.setCounselorId(counselorId);
				cim.setInfoManageId(e);
				cim.setStatus(new Byte(status));
				consultInfoManageService.updateByPrimaryKey(cim);
			}
			ResponseUtil.result(response,JsonUtil.toJsonStr("success"));
		}
	}
}
